
package xj.graph2d.app.actions;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.table.*;

import xj.graph2d.app.DrawApp;
import xj.graph2d.app.DrawAction;
import xj.graph2d.app.resources.Style;
import xj.util.gui.HTMLFrame;

import static xj.app.BaseApplicationFrame.drawFrameIcon;
import static xj.app.BaseApplicationFrame.getResourceString;
import static xj.app.BaseApplicationFrame.getSystemProperty;


public class HelpListener 
  extends DrawAction 
  implements ActionListener {

  public void actionPerformed(ActionEvent e) {
    if (e != null) {
      Object src = e.getSource();
      if (src instanceof JMenuItem) {
	if (helpFrame == null) {
	  ActionListener baction = new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
	      Object src = e.getSource();
	      if (src instanceof JButton && helpFrame != null) {
		JButton b = (JButton) src;
		String cmd = b.getActionCommand();
		doCommand(cmd);
	      }
	    }
	  };
	  JButton top = new JButton("Top");
	  top.setActionCommand("Help");
	  top.addActionListener(baction);
	  
	  //JButton topic = new JButton("Help Topics");
	  //topic.setActionCommand("HelpTopics");
	  //topic.addActionListener(baction);

	  JButton index = new JButton("Help Index");
	  index.setActionCommand("HelpIndex");
	  index.addActionListener(baction);
	  JButton back = new JButton("Back");
	  back.setActionCommand(HTMLFrame.BACK);
	  back.addActionListener(baction);
	  JButton forward = new JButton("Forward");
	  forward.setActionCommand(HTMLFrame.FORWARD);
	  forward.addActionListener(baction);

	  helpFrame = new HTMLFrame(getResourceString("HelpDialog.title"), 
				    new JButton[] { top, index, back, forward }, true,
				    900, 600);
	  helpFrame.setHTMLFont(Style.font10pt);
	  helpFrame.setIconImage(drawFrameIcon);
	  helpFrame.setResourceURL(start);
	  helpFrame.setIndexResourceURL(toc);
	}
	helpFrame.setVisible(true);
      }
    }
  }

  protected void doCommand(String cmd) {
    if (cmd != null) {
      if ("Help".equals(cmd)) {
	helpFrame.setTitle(getResourceString("HelpDialog.title"));
	helpFrame.scrollToReference("top");
      } else if ("HelpTopics".equals(cmd)) {
	helpFrame.setTitle(getResourceString("HelpTopicsDialog.title"));
	helpFrame.setResourceURL(toc);
      } else if ("HelpIndex".equals(cmd)) {
	helpFrame.setTitle(getResourceString("HelpIndexDialog.title"));
	helpFrame.setResourceURL(index);
      } else { 
	helpFrame.doCommand(cmd);
      }
    }
  }

  protected HTMLFrame helpFrame;

  protected String start = "/resources/graph2d/app/help/Guide1.html";

  protected String toc = "/resources/graph2d/app/help/HelpToc.html";

  protected String index = "/resources/graph2d/app/help/TopicIndex.html";

}

